package com.gtq.uhome.controller;

import com.gtq.uhome.result.Result;
import com.gtq.uhome.service.serviceImpl.OrderServiceImpl;

import com.gtq.uhome.util.FindStaffSiteRelations;
import com.gtq.uhome.vo.AllOrderInfor;
import com.gtq.uhome.vo.OrderInfor;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author ylhy
 * @Describe
 * @create 2020-08-06 9:21
 */
@Controller
@RequestMapping("/Order")
@Api(value = "用于所有订单管理的相关操作",tags = "总后台-->订单管理的api-->饶平")
@ResponseBody
@CrossOrigin
public class OrderController {

    @Resource(name = "OrderService")
    OrderServiceImpl orderService;

    @Transactional
    @PostMapping("/delteOrderbyLg")
    @ApiOperation(value = "删除订单信息",notes = "前端传递订单的运单id和订单id",
            httpMethod = "POST")
    public Result delteOrderbyLg(@RequestBody Map<String,Map<String,String>[]> map){

        Integer lines = 0;
        try {
            for (Map<String, String> lgId : map.get("lgId")) {
                String id = lgId.get("id");
                String lgId1 = lgId.get("lgId");
                System.out.println(id);
                System.out.println(lgId1);
                lines += orderService.delteOrderbyLg(id,lgId1);
            }
        }catch (Exception e){
            return Result.failed("删除失败,请稍后再试,超过三次系统将会联系管理员审核");
        }
        System.out.println("con中的lines : "+lines);
        if (lines != 0){
            return Result.ok("删除成功");
        }else {
            return Result.failed("删除失败,请稍后再试,超过三次系统将会联系管理员审核");
        }
    }

    @PostMapping("/findOderByLg")
    @ApiOperation(value = "查询单个物流订单信息",notes = "前端传递订单的运单id",
            httpMethod = "POST")
    public Result findOneOrderList(@RequestBody Map map){

        String logisticsId = (String) map.get("logisticsId");

        System.out.println("查詢单挑物流单向请");
        OrderInfor orderInfor = null;

        try {
            orderInfor = orderService.findOneOrderListByOrderLg(logisticsId);
        }catch (Exception e){
            return Result.failed("数据库维护,系统已向管理员反馈");
        }

        if (orderInfor == null ){
            return Result.failed("数据库维护,系统已向管理员反馈");
        }else {
            return Result.ok(orderInfor,"查询成功");
        }
    }

    /**
     * 查詢所有订单信息（已物流单分组）
     * @return
     */
    @PostMapping("/findOrderList")
    @ApiOperation(value = "查询所有订单信息订单信息",notes = "为总后台传递信息前端无需传值",
            httpMethod = "POST")
    public Result findAllOrderList(){
        List<AllOrderInfor> allOrderListByOrderLg = null;

        try {
            allOrderListByOrderLg = orderService.findAllOrderListByOrderLg();
        }catch (Exception e){
            System.out.println(e);
            return Result.failed("无有效定单,请联系站点");
        }

        if (allOrderListByOrderLg!=null){
            return Result.ok(allOrderListByOrderLg,"查询成功");
        }else {
            return Result.failed("无有效定单,请联系站点");
        }
    }

    @PostMapping("/findAllOrderListBySite")
    @ApiOperation(value = "根据站点id查询所有订单信息订单信息",notes = "为站点后台传递信息前端需传值为站点的id",
            httpMethod = "POST")
    public Result findAllOrderListBySite(@RequestBody Map map){
        Integer siteId = (Integer) map.get("siteId");

        List<AllOrderInfor> allOrderListByOrderLgFromSite = null;

        try {
            allOrderListByOrderLgFromSite = orderService.findAllOrderListByOrderLgFromSite(siteId);
        }catch (Exception e){
            System.out.println(e);
            return Result.failed("无有效定单,请联系站点");
        }

        if (allOrderListByOrderLgFromSite!=null){
            return Result.ok(allOrderListByOrderLgFromSite,"查询成功");
        }else {
            return Result.failed("无有效定单,请联系站点");
        }
    }


    @PostMapping("/findSomeOrderList")
    @ApiOperation(value = "模糊查询查询订单信息订单信息",notes = "为总后台传递信息前端需传值 keyWords deliveryTimestart deliveryTimeend " +
            "checkStatus siteName",
            httpMethod = "POST")
    public Result findSomeOrderList(@RequestBody Map map){
        String keyWords = (String) map.get("keyWords");
        String deliveryTimestart = (String) map.get("deliveryTimestart");
        String deliveryTimeend = (String) map.get("deliveryTimeend");
        String checkStatus = (String) map.get("checkStatus");
        String siteName = (String) map.get("siteName");


        //通用接口的增加操作
        String agingCode = (String) map.get("agingCode");
        String singCode = (String) map.get("singCode");

        System.out.println("-----deliveryTimestart-------"+deliveryTimestart);
        System.out.println("-----deliveryTimeend-------"+deliveryTimeend);

        List<AllOrderInfor> someOrderListByOrderLg = null;

        try {

            someOrderListByOrderLg = orderService.findSomeOrderList(keyWords,deliveryTimestart,
                    deliveryTimeend,checkStatus,siteName,singCode,agingCode);

        }catch (Exception e){
            return Result.failed("服务器正遭受攻击,管理员正在维护");
        }
        if (someOrderListByOrderLg == null){
            return Result.failed("无符合条件数据");
        }else {
            return Result.failed(someOrderListByOrderLg,"查询成功");
        }
    }
}
